/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-22 16:19:19
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-06 09:24:16
 * @FilePath: \cxl-driving-training\src\views\clueManagement\clue\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { DICT_DATA } from '@/utils/dict-data'
import { userDisable, userPage, userPageInterface } from '@/apis/company/user'

export default class UserData extends BaseData implements BaseMethod {
    followStatus = reactive<{ label: string; value: string }[]>([])
    searchData = {
        name: '',
        account: '',
        status: '',
        userType: ''
    }
    //编辑用户
    operationDialog = reactive({
        visible: false,
        type: 'create',
        id: '',
        name: '',
        account: '',
        userType: ''
    })
    //授权角色
    authorizationDialog = reactive({
        visible: false,
        id: ''
    })
    //重置密码
    resetPasswordDialog = reactive({
        visible: false,
        id: ''
    })
    constructor() {
        super(tableConfig)
        this.getData()
    }
    getData = (arg?: any) => {
        const params: userPageInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        if (this.searchData.name) {
            params.name = this.searchData.name
        }
        if (this.searchData.account) {
            params.account = this.searchData.account
        }
        if (this.searchData.status) {
            params.disable = Number(this.searchData.status)
        }
        if (this.searchData.userType) {
            params.userType = Number(this.searchData.userType)
        }
        userPage(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total
                const arr: any[] = res.data.result.records
                arr.forEach((element: any) => {
                    element.status = Number(element.disable) === 0 ? true : false

                    element.roleList = element.roles.map((item: any) => {
                        return item.name
                    })
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.searchData = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.searchData = {
            name: '',
            account: '',
            status: '',
            userType: ''
        }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type, data)
        if (type === 'edit') {
            this.operationDialog.visible = true
            this.operationDialog.type = type
            this.operationDialog.id = data.id
            this.operationDialog.name = data.name
            this.operationDialog.account = data.account
            this.operationDialog.userType = data.userType
        } else if (type === 'create') {
            this.operationDialog.visible = true
            this.operationDialog.type = type
            this.operationDialog.id = ''
            this.operationDialog.name = ''
            this.operationDialog.account = ''
            this.operationDialog.userType = '1'
        } else if (type === 'authorization') {
            this.authorizationDialog.visible = true
            this.authorizationDialog.id = data.id
        } else if (type === 'resetPassword') {
            this.resetPasswordDialog.visible = true
            this.resetPasswordDialog.id = data.id
        } else if (type === 'switch') {
            userDisable(data.id, Number(data.disable) === 0 ? 1 : 0).then(res => {
                if (Number(res.data.code) === 200) {
                    //
                }
            })
        }
    }
    //监听弹窗关闭
    onCloseOperationDialog = () => {
        this.operationDialog.visible = false
        this.getData()
    }
    //监听授权角色关闭
    onCloseAuthorizationDialog = () => {
        this.authorizationDialog.visible = false
        this.getData()
    }
    //监听重置密码关闭
    onCloseResetPasswordDialog = () => {
        this.resetPasswordDialog.visible = false
    }
}
